<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Config;

class CreditController extends Controller
{
    // 账号
    const ACCOUNT = 'api-ceshi-jingzhungoutong';
    // 私钥
    const PRIVATEKEY = 'a0230b6383e18482';
    // 接入方生成的流水号
    private $reqTid;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->reqTid = time() . rand(10000, 99999);
    }

    /**
     * 个人综合信用评分T2接口
     * $mobile string 手机号
     * $cid string 身份证号
     * $name string 姓名
     */
    function scoreT1(Request $request) {
        //参数验证
        $messages = [
            'required'      => ':attribute 项是必要的。',
            'file'          => ':attribute 上传数据必须是文件',
        ];

        $this->validate($request, [
            'mobile'        => 'required|size:11',
            'cid'           => 'required',
            'name'          => 'required'
        ], $messages);

        $data = [];
        $data['account'] = self::ACCOUNT;
        $data['reqTid'] = $this->reqTid;
        $data['mobile'] = $request->mobile;
        $data['cid'] = $request->cid;
        $data['name'] = $request->name;
        $data['sign'] = $this->getCreditSign($data, self::PRIVATEKEY);

        $url = env('CREDIT_URL') . 'credit/score/t1';

        $res = $this->curl($url, $data);

        if ($res) {
            return ['status' => 0, 'data' => json_decode($res, true)];
        } else {
            return ['status' => 1, 'msg' => '请求中诚信接口失败'];
        }
    }

    /**
     * 个人综合信用评分(五维度评分)接口
     * $mobile string 手机号
     * $cid string 身份证号
     * $name string 姓名
     * $card string 银行卡号
     */
    public function scoreGeneral(Request $request)
    {
        //参数验证
        $messages = [
            'required'      => ':attribute 项是必要的。',
            'file'          => ':attribute 上传数据必须是文件',
        ];

        $this->validate($request, [
            'mobile'        => 'required|size:11',
            'cid'           => 'required',
            'name'          => 'required',
            'card'          => 'required'
        ], $messages);

        $data = [];
        $data['account'] = self::ACCOUNT;
        $data['mobile'] = $request->mobile;
        $data['cid'] = $request->cid;
        $data['name'] = $request->name;
        $data['card'] = $request->card;
        $data['sign'] = $this->getCreditSign($data, self::PRIVATEKEY);

        $url = env('CREDIT_URL') . 'credit/score/general/factors';

        $res = $this->curl($url, $data);
        // echo $res;die;

        if ($res) {
            return ['status' => 0, 'data' => json_decode($res, true)];
        } else {
            return ['status' => 1, 'msg' => '请求中诚信接口失败'];
        }
    }

    /**
     * 金融画像信息T2接口
     * $mobile string 手机号
     * $cid string 身份证号
     * $name string 姓名
     */
    public function portraitFinance(Request $request)
    {
        //参数验证
        $messages = [
            'required'      => ':attribute 项是必要的。',
            'file'          => ':attribute 上传数据必须是文件',
        ];

        $this->validate($request, [
            'mobile'        => 'required|size:11',
            'cid'           => 'required',
            'name'          => 'required'
        ], $messages);

        $data = [];
        $data['account'] = self::ACCOUNT;
        $data['reqTid'] = $this->reqTid;
        $data['mobile'] = $request->mobile;
        $data['cid'] = $request->cid;
        $data['name'] = $request->name;
        $data['sign'] = $this->getCreditSign($data, self::PRIVATEKEY);

        $url = env('CREDIT_URL') . 'portrait/finance/t2';

        $res = $this->curl($url, $data);
        // print_r($res);die;

        if ($res) {
            return ['status' => 0, 'data' => json_decode($res, true)];
        } else {
            return ['status' => 1, 'msg' => '请求中诚信接口失败'];
        }
    }
}
